<?php
namespace App\Model;

use lib\Model;

class Image extends Model
{
    private $id;
    private $title;
    private $description;
    private $path;
    private $uploaded_at;
    private $updated_at;
    private $deleted_at;

    public function __construct($id, $title, $path, $description = '')
    {
        $this->id = $id;
        $this->title = $title;
        $this->path = $path;
        $this->description = $description;
        $this->uploaded_at = time();
        $this->updated_at = $this->uploaded_at;
        $this->deleted_at = null;
    }

    public static function fromArray(Array $array)
    {
        if (empty($array) || count($array) == 0) {
            return null;
        }
        return new Image($array['id'], $array['title'], $array['path'], $array['description']);
    }

    public function toArray()
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'path' => $this->path,
            'description' => $this->description,
            'uploaded_at' => $this->uploaded_at,
            'updated_at' => $this->updated_at,
            'deleted_at' => $this->deleted_at
        ];
    }

    public function __get($name)
    {
        if (property_exists(self::class, $name)) {
            return $this->$name;
        }
        return null;
    }

    public function __set($name, $value)
    {
        if (property_exists(self::class, $name)) {
            $this->$name = $value;
        }
    }
}
